<?php

App::uses('AppController', 'Controller');

class BillsController extends AppController {
	
	var $name = 'Bills';

	var $uses = array('Bill', 'BillDetail');
	
	/**
	 * List Bills
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_index()
	{
		App::uses('User', 'Model');
		$User = new User();

		$this->set('bills', $this->Bill->getAll());
		$this->set('als', 'Bill');
		$this->set('employees', $User->getListByRole(3));
	}

	/**
	 * Add
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_add($id = null)
	{
		$index = 0;
		if(!empty($this->request->data))
		{
			$data = &$this->request->data;

			// Tinh payment
			App::uses('Product', 'Model');
			$Product = new Product();

			$sum = 0;
			$details = array();
			if(!empty( $data['DetailBill'] )) {
				foreach($data['DetailBill'] as $dt)
				{
					$dt['amount'] = str_replace(array('.', ',', ' '), '', $dt['amount']);
					$product = $Product->getInfoById((int)$dt['product_id']);
					$sum += $product['Product']['price'] * (int)$dt['amount'];

					$product['Product']['amount'] = $dt['amount'];
					$details[$index] = $product['Product'];
					$index++;
				}
			} else {
				$this->Bill->validationErrors['product'] = 'Please, enter the product.';
			}

			$data['Bill']['sum'] = $sum;
			$data['Bill']['get_payment'] = (float)str_replace(array('.', ',', ' '), '', $data['Bill']['get_payment']);
			$data['Bill']['discount'] = (int)str_replace(array('.', ',', ' '), '', $data['Bill']['discount']);
			$data['Bill']['remaining'] = $sum - (($sum * $data['Bill']['discount'])/100 + $data['Bill']['get_payment']);

			// Kiem tra errors
			$this->Bill->set($data['Bill']);
			$errors = $this->Bill->invalidFields();

			//Save
			if(empty($errors)) {

				// kiem tra edit
				if( empty($data['Bill']['id']) ) {
					unset($data['Bill']['id']);
				}

				$this->Common->get_common_info($data['Bill']);

				if($this->Bill->save($data['Bill'], false)) {

					// Save Bill detai
					foreach($data['DetailBill'] as $dt)
					{
						$dt['bill_id'] = $this->Bill->id;
						$dt['status'] = 4;
						$this->BillDetail->create();
						$this->BillDetail->save($dt);
					}

					$this->Session->setFlash('Save bill success.', 'success');
					$this->redirect('/admin/bills');
				} else {
					$this->Session->setFlash('Save bill fail.', 'error');
				}
			} else {
				$this->set('errors', $errors);
				$this->set('details', $details);
				$this->Session->setFlash('Save user fail.', 'error');
			}
		}

		// Get employee
		App::uses('User', 'Model');
		$User = new User();

		$this->set('user', $this->Auth->User('fullname'));
		$this->set('employees', $User->getListByRole(3));
		$this->set('index', $index);
	}
	
	/**
	 * Detail bill
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_view($id = null) 
	{
		// Neu ko co id thi redirect index
		if(empty($id)) {
			$this->Session->setFlash('Not found the bill.', 'error');
			$this->redirect('/admin/bills');
		}

		// Get info bill
		$bill = $this->Bill->getInfoById($id);
		$this->set('bill', $bill);
	}
}
